﻿using LogUtility;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Api
{
    public class RobotApiHelper
    {
        string robotApi = ConfigurationManager.AppSettings["robotApi"];


        /// <summary>
        /// 更新pid
        /// </summary>
        /// <param name="alimamaid"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public string UpdatePid(string alimamaid, string username)
        {
            string apiUrl = robotApi + "/V2/TK/SelfAdzoneCreate";
            var param = string.Format("?account={0}&username={1}", alimamaid, username);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch(Exception ex)
            {
                LogHelper.Error("UpdatePid:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }



            return response;
        }

        /// <summary>
        /// 更新指定推广者群管配置
        /// </summary>
        /// <param name="alimamaid"></param>
        /// <returns></returns>
        public string UpdateSysConfig(string promoter_id)
        {
            string apiUrl = robotApi + "/V2/TK/UpdateSysManagementConfig";
            var param = string.Format("?promoter_id={0}", promoter_id);

            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("UpdateSysConfig:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }
            return response;
        }
        /// <summary>
        /// 更新指定推广者发单配置
        /// </summary>
        /// <param name="alimamaid"></param>
        /// <returns></returns>
        public string UpdateSendtConfig(string promoter_id)
        {
            string apiUrl = robotApi + "/V2/TK/UpdateSendtConfig";
            var param = string.Format("?promoter_id={0}", promoter_id);

            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("UpdateSendtConfig:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }
            return response;
        }



        /// <summary>
        /// 消息群发
        /// </summary>
        /// <param name="alimamaid"></param>
        /// <returns></returns>
        public string SendMessaging(string promoter_id, string AU_NameS, string picUrl, string sendContent)
        {
            string apiUrl = robotApi + "/V2/TK/SendMessaging";
            var param = string.Format("?promoter_id={0}&AU_NameS={1}&picUrl={2}&sendContent={3}", promoter_id, AU_NameS, picUrl, sendContent);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("SendMessaging:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }
            
            return response;
        }

        /// <summary>
        /// 更新代理配置信息
        /// </summary>
        /// <param name="promoter_id"></param>
        /// <param name="AU_Name"></param>
        /// <returns></returns>
        public string UpdateAgentConfig(string promoter_id, string AU_Name, string FlockName)
        {
            string apiUrl = robotApi + "/V2/TK/UpdateAgentConfig";
            var param = string.Format("?promoter_id={0}&AU_Name={1}&FlockName={2}", promoter_id, AU_Name, FlockName);
            var response= "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("UpdateAgentConfig:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }
            
            return response;
        }



        /// <summary>
        /// 更新代理配置信息
        /// </summary>
        /// <param name="promoter_id"></param>
        /// <param name="AU_Name"></param>
        /// <returns></returns>
        public string AddAgentBind(string promoter_id, string AU_Name, string FlockName)
        {
            string apiUrl = robotApi + "/V2/TK/AddAgentBind";
            var param = string.Format("?promoter_id={0}&AU_Name={1}&FlockName={2}", promoter_id, AU_Name, FlockName);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("AddAgentBind:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }

        /// <summary>
        /// //单品转链群发
        /// </summary>
        /// <param name="au_id">代理id</param>
        /// <param name="auctionId">商品id</param>
        /// <param name="?"></param>
        /// <returns></returns>
        public string ItemChainGroup(string au_id, string auctionId)
        {
            string apiUrl = robotApi + "/V2/TK/ItemChainGroup";
            var param = string.Format("?au_id={0}&auctionId={1}", au_id,auctionId);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("ItemChainGroup:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }


      
        /// <summary>
        /// 单品转链
        /// </summary>
        /// <param name="promoter_id"></param>
        /// <param name="au_name"></param>
        /// <param name="auctionId"></param>
        /// <returns></returns>
        public string FrontItemChain(string promoter_id,string au_name , string auctionId)
        {
            string apiUrl = robotApi + "/V2/TK/FrontItemChain";
            var param = string.Format("?promoter_id={0}&au_name={1}&auctionId={2}", promoter_id, au_name, auctionId);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("FrontItemChain:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }


        /// <summary>
        /// 单品群发
        /// </summary>
        /// <param name="au_id">代理ID</param>
        /// <param name="auctionId">商品ID</param>
        /// <returns></returns>
        public string FrontItemSends(string promoter_id, string au_name, string auctionId)
        {
            string apiUrl = robotApi + "/V2/TK/FrontItemSends";
            var param = string.Format("?promoter_id={0}&au_name={1}&auctionId={2}", promoter_id, au_name, auctionId);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("FrontItemSends:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }

        /// <summary>
        /// 超级搜索转链
        /// </summary>
        /// <param name="account">阿里妈妈ID</param>
        /// <param name="auctionid">商品ID</param>
        /// <param name="adzoneid">推广位ID</param>
        /// <param name="sitename">推广位类型</param>
        /// <param name="includeDxjh">定向计划(0没定向计划,1反之)</param>
        /// <param name="eventRate">高佣佣金比率</param>
        /// <param name="tkRate">通用佣金比率</param>
        /// <param name="tkSpecialCampaignIdRateMap">定向计划列表</param>
        /// <returns></returns>
        public string SuperSearch(string account, string auctionid, string adzoneid, int includeDxjh, string eventRate, string tkRate, string tkSpecialCampaignIdRateMap, string sitename="wz")
        {
            string apiUrl = robotApi + "/V2/TK/SuperSearch";
            var param = string.Format("?account={0}&auctionid={1}&adzoneid={2}&sitename={7}&includeDxjh={3}&eventRate={4}&tkRate={5}&tkSpecialCampaignIdRateMap={6}", account, auctionid, adzoneid, includeDxjh, eventRate, tkRate, tkSpecialCampaignIdRateMap, sitename);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);

            }
            catch (Exception ex)
            {
                LogHelper.Error("FrontItemSends:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }
            return response;
        }
        public string AddAgentWxSendRobot(string promoter_id, string au_name, string Au_Id, string FlockNames)
        {
            string apiUrl = robotApi + "/V2/TK/AddAgentWxSendRobot";
            var param = string.Format("?promoter_id={0}&au_name={1}&Au_Id={2}&FlockNames={3}", promoter_id, au_name, Au_Id, FlockNames);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);


            }

            catch (Exception ex)
            {
                LogHelper.Error("AddAgentWxSendRobot:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }


  
        

       /// <summary>
        /// 营销计划申请
       /// </summary>
       /// <param name="account"></param>
       /// <param name="auctionid"></param>
       /// <param name="adzoneid"></param>
        /// <param name="sitename">媒体推广类型[wz,wx,qq] </param>
       /// <returns></returns>
        public string getMarketingAuctionCode(string account, string auctionid, string adzoneid, string sitename)
        {
            string apiUrl = robotApi + "/V2/TK/getMarketingAuctionCode";
            var param = string.Format("?account={0}&auctionid={1}&adzoneid={2}&sitename={3}", account, auctionid, adzoneid, sitename);
            var response = "";
            try
            {
                response = XYD.Common.Utils.GetHttpWebResponse(apiUrl + param);


            }

            catch (Exception ex)
            {
                LogHelper.Error("getMarketingAuctionCode:" + apiUrl + param, ex);
                response = "{ state = 0, msg = \"服务器异常请联系管理员\" }";
            }

            return response;
        }


    }
}
